package com.lzy.cloud.search.client.service.impl;

import com.lzy.cloud.search.client.service.MetricsAggregationsService;
import com.lzy.cloud.search.client.util.EsClient;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.max.Max;
import org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.min.Min;
import org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.sum.Sum;
import org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 指标聚合
 */
@Service
public class MetricsAggregationsServiceImpl implements MetricsAggregationsService {

    @Autowired
    private EsClient client;

    public void testMinAggregation()   {
        MinAggregationBuilder aggregation =
                AggregationBuilders
                        .min("agg")
                        .field("age");

        SearchResponse sr = client.getConnection().prepareSearch("twitter")
                .addAggregation(aggregation)
                .get();

        // sr is here your SearchResponse object
        Min agg = sr.getAggregations().get("agg");
        String value = agg.getValueAsString();//这个统计的是日期，一般用下面方法获得最小值

//        double value = agg.getValue();

        System.out.println("min value:" + value);
    }

    public void testMaxAggregation()   {
        MaxAggregationBuilder aggregation =
                AggregationBuilders
                        .max("agg")
                        .field("age");


        SearchResponse sr = client.getConnection().prepareSearch("twitter")
                .addAggregation(aggregation)
                .get();

        // sr is here your SearchResponse object
        Max agg = sr.getAggregations().get("agg");
//        double value = agg.getValue();

        String value = agg.getValueAsString();

        System.out.println("max value:" + value);
    }

    public void testSumAggregation()   {
        SumAggregationBuilder aggregation =
                AggregationBuilders
                        .sum("agg")
                        .field("age");

        SearchResponse sr = client.getConnection().prepareSearch("twitter")
                .addAggregation(aggregation)
                .get();


        // sr is here your SearchResponse object
        Sum agg = sr.getAggregations().get("agg");
//        double value = agg.getValue();

        String value = agg.getValueAsString();

        System.out.println("sum value:" + value);
    }

    public void testAvgAggregation()   {

    }

    public void testStatsAggregation()   {

    }

    public void testExtendedStatsAggregation()   {

    }

    public void testValueCountAggregation()   {

    }

    public void testPrepareAggregation()   {

    }

    public void testPrepareRanksAggregation()   {

    }

    public void testCardinalityAggregation()   {

    }

    public void testGeoBoundsAggregation()   {

    }

    public void testTopHitsAggregation()   {

    }

    public void testScriptedMetricAggregation()   {

    }
}
